package com.sugo.seckill.web.config;

import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11NioProtocol;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.stereotype.Component;

/**
 * <p>设置web服务</p>
 *
 * @author langxing
 * @date 2020/12/20 15:51
 */
@Component
public class WebServerConfig implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {

    /**
     * 设置keep alive相关配置
     * @param factory
     * @author linhx
     * @date 2020/12/20 16:00
     */
    @Override
    public void customize(ConfigurableWebServerFactory factory) {
        // 使用对应工程类定制tomcat connector
        ((TomcatServletWebServerFactory) factory).addConnectorCustomizers(new TomcatConnectorCustomizer() {
            @Override
            public void customize(Connector connector) {
                // 获取httpNio(springboot内置tomcat默认使用nio)
                Http11NioProtocol http11NioProtocol = (Http11NioProtocol) connector.getProtocolHandler();
                // 设置keep alive 连接数量
                http11NioProtocol.setMaxKeepAliveRequests(10000);
                // 设置keep alive 连接的超时时间
                http11NioProtocol.setKeepAliveTimeout(300000);
            }
        });
    }
}
